package cn.zmit.kuxi.activity;

import java.net.URLEncoder;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.xdroid.common.utils.ToastUtils;
import com.xdroid.functions.holder.ListViewDataAdapter;
import com.xdroid.functions.request.JsonData;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;

import cn.zmit.kuxi.R;
import cn.zmit.kuxi.entity.GoodsEnity;
import cn.zmit.kuxi.holder.SearchHolder;
import cn.zmit.kuxi.interfaces.OnListItemSelectListener;
import cn.zmit.kuxi.request.RequestTask;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

/** * @author @parameter * @return */
public class SearchActivity extends Activity {
	@ViewInject(R.id.editext_keywords)
	private EditText editext_keywords;
	@ViewInject(R.id.list_search)
	private ListView list_search;
	@ViewInject(R.id.imag)
	private ImageView imag;
	private String key=null;
	private ListViewDataAdapter<GoodsEnity>goodsAdapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_search);
		ViewUtils.inject(this);
		initView();
	}
	private void initView(){
		goodsAdapter=new ListViewDataAdapter<GoodsEnity>();
		goodsAdapter.setViewHolderClass(this, SearchHolder.class);
		list_search.setAdapter(goodsAdapter);
		SearchHolder.setOnListItemSelectListener(new OnListItemSelectListener<GoodsEnity>() {
			
			@Override
			public void onlistItemSelect(int position, GoodsEnity itemDate) {
				Intent intent =new Intent();
				Bundle bundle=new Bundle();
				bundle.putString("gp_id", itemDate.getGp_id());
				intent.putExtras(bundle);
				intent.setClass(SearchActivity.this, GoodsDetailActivity.class);
				startActivity(intent);
			}
		});
		editext_keywords.setOnEditorActionListener(new OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if(actionId ==EditorInfo.IME_ACTION_SEARCH){
					// 先隐藏键盘
					((InputMethodManager) editext_keywords.getContext().getSystemService(Context.INPUT_METHOD_SERVICE))
					.hideSoftInputFromWindow(
					SearchActivity.this
					.getCurrentFocus()
					.getWindowToken(),
					InputMethodManager.HIDE_NOT_ALWAYS);
					String conten=editext_keywords.getText().toString().trim();
			
					RequestTask.getInstance().getMainSerach(SearchActivity.this, URLEncoder.encode(conten),new onRequestListener());
					return true;
				}
				
				
				return false;
			}
		});
	}
private class onRequestListener extends OnRequestListenerAdapter<Object>{
	@Override
	public void onDone(XDroidRequest<?> request, String response, DataType dataType) {
		// TODO Auto-generated method stub
		super.onDone(request, response, dataType);
		goodsAdapter.removeAll();
		JsonData jsonData=JsonData.create(response);
		JsonData data=jsonData.optJson("goods");
		if (data!=null&&data.length()>0) {
//			ToastUtils.show(SearchActivity.this, jsonData.optString("message"));
			for (int i = 0; i < data.length(); i++) {
				JsonData itemData=data.optJson(i);
				String buy_count=itemData.optString("buy_count");
				String gp_id=itemData.optString("gp_id");
				String phase=itemData.optString("phase");
				String name=itemData.optString("name");
				String goods_id=itemData.optString("goods_id");
				String allNeed=itemData.optString("price");
				String url=itemData.optString("pic");
				GoodsEnity goodsEnity=new GoodsEnity(phase, name, allNeed, buy_count, url, goods_id, gp_id);
				goodsAdapter.append(goodsEnity);
			}
		}else {
//			ToastUtils.show(SearchActivity.this, jsonData.optString("message"));
		}
		
	}
}
	
	
}
